package com.morse.player

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.exoplayer.drm.DefaultDrmSessionManager
import androidx.media3.exoplayer.drm.ExoMediaDrm
import androidx.media3.exoplayer.drm.MediaDrmCallback
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector
import androidx.media3.ui.PlayerView
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.UUID

@UnstableApi
class TestPlayerActivity : AppCompatActivity() {

    private var playerView: PlayerView? = null
    private var player: ExoPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test_player)

        playerView = findViewById(R.id.pv_player)

        initPlayer()
    }

    @OptIn(DelicateCoroutinesApi::class)
    override fun onResume() {
        super.onResume()
        GlobalScope.launch {
            player?.addListener(object : Player.Listener {
                override fun onPlaybackStateChanged(playbackState: Int) {
                    when (playbackState) {
                        ExoPlayer.STATE_READY -> {
                            println("播放器已准备好")
                        }

                        ExoPlayer.STATE_IDLE -> {
                            println("播放器空闲")
                        }

                        ExoPlayer.STATE_BUFFERING -> {
                            println("播放器缓存中")
                        }

                        ExoPlayer.STATE_ENDED -> {
                            println("播放完成")
                        }
                    }
                }

                override fun onPlayerError(error: PlaybackException) {
                    println("播放错误：${error.message}")
                }
            })
        }
    }

    fun initPlayer() {
        val selector = DefaultTrackSelector(this).apply {
            parameters = buildUponParameters().setMaxVideoSizeSd().build()
        }
        val drm = DefaultDrmSessionManager.Builder()
            .setKeyRequestParameters(mapOf("Content-Type" to "application/json"))
            .build(object : MediaDrmCallback {
                override fun executeProvisionRequest(
                    uuid: UUID, request: ExoMediaDrm.ProvisionRequest
                ): ByteArray {
                    TODO("Not yet implemented")
                }

                override fun executeKeyRequest(
                    uuid: UUID, request: ExoMediaDrm.KeyRequest
                ): ByteArray {
                    TODO("Not yet implemented")
                }

            })
        player = ExoPlayer.Builder(this).setTrackSelector(selector).build().also {
                playerView?.player = it
                val mediaItem =
                    MediaItem.fromUri("android.resource://" + packageName + "/" + R.raw.sv_ad_video)
                it.setMediaItem(mediaItem)
                it.prepare()
                it.playWhenReady = true
            }
    }

    override fun onDestroy() {
        super.onDestroy()
        player?.release()
        player = null
    }
}